package com.devgroupvn.dota;

import java.io.IOException;
import java.util.ArrayList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


public class WebContent {
	//private final String URL = "http://www.joindota.com/en/vods";
	private final int TIMEOUT = 10000;
	private final String ENCODE_UTF_8 = "<html><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /></html>";
	
	// constructure
	public WebContent() {
		/*
		try {
			// using Jsoup to load content
	        Document doc = Jsoup.connect(URL).timeout(TIMEOUT).get();
	        //String title = doc.title();
	        //System.out.println(title);
	        
	        Elements links = doc.select("h2");
	        for (Element link : links) {
	        	//Document mdoc = Jsoup.parse(link.html());
	        	
	        	//Log.d("mydebug2",mdoc.select("a").first().attr("href") + "***");
	        	
	        }
	        
	        
		} catch (Exception e) {
			Log.d("eee",e.toString());
		}
		*/
	}
	
	
	// get html content from post link
	public String getPostContent(String url) throws IOException {
		//Log.d("huhu","toi day 1");
		Document doc = Jsoup.connect(url).timeout(TIMEOUT).get();
		//Log.d("huhu","toi day 2");
		Element object = doc.select("object").first();
		String source = object.select("embed").first().attr("src");
		// http://www.own3d.tv/stream/648345
		// http://www.own3d.tv/DotA/watch/648345
		// http://vodcdn.ec.own3d.tv/videos/SD/648000/648345_4fc275030ee3b_HQ.mp4?7418afca70ba1dc09fb6b6e37c287072169117b285d7dfcce5f8d90044573fdc81f9&ec_seek=0&ec_rate=350&ec_prebuf=5
		
		source = source.replace("stream", "DotA/watch");
		//Log.d("kakakka",source);
		
		doc = Jsoup.connect(source).timeout(TIMEOUT).get();
		String content = doc.html();
		String filePath = "";
        String queryString = "";
        int start = content.indexOf("HQUrl:");
        int end = content.indexOf(",", start);
        filePath = content.substring(start+8, end-1);
        
        start = content.indexOf("queryString: escape(");
        end = content.indexOf(")", start);
        queryString = content.substring(start+21, end-1);
        
        String finalresult = "http://vodcdn.ec.own3d.tv/"+ filePath + queryString ; //+ "&ec_seek=0&ec_rate=350&ec_prebuf=5";
        //System.out.println(finalresult);
        finalresult = finalresult.replace("${start}", "0");
        //finalresult = finalresult.replace("&ec_seek=${start}", "");
        
        return finalresult;
        /*
        myVideoView.setVideoURI(Uri.parse(finalresult));
        myVideoView.setMediaController(new MediaController(this));
        myVideoView.requestFocus();
        myVideoView.start(); 
		*/
	}
	
	
	// get list post url
	
	public ArrayList<Post> getPostList(String url) throws IOException {
		Document doc = Jsoup.connect(url).timeout(TIMEOUT).get();
        
		ArrayList<Post> listpost = new ArrayList<Post>();
        
        Elements links = doc.select("div.vod_item");
        
        for (Element link : links) {
        	Post post = new Post();
        	post.title = link.select("strong").first().text();
        	post.link = link.select("a").first().attr("href");
        	post.img =	link.select("img").first().attr("src");
        	post.description = link.select("span").last().text();
        	post.date = link.select("span").first().text()+ " " +link.select("span").get(1).text() ;
        	
        	listpost.add(post);
        }
        
        return listpost;
	}	

}
